package com.sunday.shoufen.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.sunday.common.base.BaseActivity;
import com.sunday.shoufen.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 刘涛 on 2017/11/6.
 */

public class SearchShopListActivity extends BaseActivity {

    @Bind(R.id.btn_back)
    ImageView btnBack;
    @Bind(R.id.edit_query)
    EditText editQuery;
    @Bind(R.id.slide_tab)
    SlidingTabLayout tabLayout;
    @Bind(R.id.view_pager)
    ViewPager viewPager;

    private List<String> titles = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_searh_shop_list);
        ButterKnife.bind(this);
        initView();
    }

    String name;
    private void initView(){
        titles.add("综合排序");
        titles.add("销量");
        titles.add("评价");
         /*隐藏软键盘*/

        viewPager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
        tabLayout.setViewPager(viewPager);
        viewPager.setCurrentItem(0, false);
        viewPager.setOffscreenPageLimit(titles.size());

        editQuery.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                name = String.valueOf(s);
                ((SearchShopListFragment) (fragments[viewPager.getCurrentItem()])).setSeachStr(name);
                ((SearchShopListFragment) (fragments[viewPager.getCurrentItem()])).refresh();
                /*InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (inputMethodManager.isActive()) {
                    inputMethodManager.hideSoftInputFromWindow(editQuery.getApplicationWindowToken(), 0);
                }*/
            }
        });

        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                if (!TextUtils.isEmpty(name)){
                    ((SearchShopListFragment) (fragments[viewPager.getCurrentItem()])).setSeachStr(name);
                    ((SearchShopListFragment) (fragments[viewPager.getCurrentItem()])).refresh();
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });


    }


    private Fragment[] fragments;

    public class PagerAdapter extends FragmentPagerAdapter {


        public PagerAdapter(FragmentManager fm) {
            super(fm);
            fragments = new Fragment[titles.size()];
        }

        @Override
        public Fragment getItem(int position) {
            if (fragments[position] == null) {
                fragments[position] = SearchShopListFragment.newInstance(position);
            }
            return fragments[position];
        }

        @Override
        public int getCount() {
            return titles.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }
    }



}
